package cn.tedu.web;

import cn.tedu.dao.UserDAO;
import cn.tedu.dao.UserDAOImpl;
import cn.tedu.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/addUser")
public class UserServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        String name = request.getParameter("name");
        System.out.println("-------"+name);
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //入库操作
        User user = new User(name,username,password);
        UserDAO dao = new UserDAOImpl();
        int row = dao.insert(user);
        if(row!=1){
            out.write("注册失败");
        }else{
            out.write("注册成功 <a href='login.html' >登录</a>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
